package com.itheima.security.distributed.gateway.common;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.bcrypt.BCrypt;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class EncryptUtil {
    private static final Logger logger = LoggerFactory.getLogger(EncryptUtil.class);

    public static String encodeBase64(byte[] bytes){
        String encoded = Base64.getEncoder().encodeToString(bytes);
        return encoded;
    }

    public static byte[]  decodeBase64(String str){
        byte[] bytes = null;
        bytes = Base64.getDecoder().decode(str);
        return bytes;
    }

    public static String encodeUTF8StringBase64(String str){
        String encoded = null;
        try {
            encoded = Base64.getEncoder().encodeToString(str.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            logger.warn("不支持的编码格式",e);
        }
        return encoded;

    }

    public static String  decodeUTF8StringBase64(String str){
        String decoded = null;
        byte[] bytes = Base64.getDecoder().decode(str);
        try {
            decoded = new String(bytes,"utf-8");
        }catch(UnsupportedEncodingException e){
            logger.warn("不支持的编码格式",e);
        }
        return decoded;
    }

    public static String encodeURL(String url) {
    	String encoded = null;
		try {
			encoded =  URLEncoder.encode(url, "utf-8");
		} catch (UnsupportedEncodingException e) {
			logger.warn("URLEncode失败", e);
		}
		return encoded;
	}


	public static String decodeURL(String url) {
    	String decoded = null;
		try {
			decoded = URLDecoder.decode(url, "utf-8");
		} catch (UnsupportedEncodingException e) {
			logger.warn("URLDecode失败", e);
		}
		return decoded;
	}

    public static void main(String [] args) throws UnsupportedEncodingException {
        String str = "abcd{'a':'b'}";
        String encoded = EncryptUtil.encodeUTF8StringBase64(str);
        String decoded = EncryptUtil.decodeUTF8StringBase64(encoded);
        System.out.println(str);
        System.out.println(encoded);
        System.out.println(decoded);

        String url = "== wo";
        String urlEncoded = EncryptUtil.encodeURL(url);
        String urlDecoded = EncryptUtil.decodeURL(urlEncoded);
        
        System.out.println(url);
        System.out.println(urlEncoded);
        System.out.println(urlDecoded);

        System.err.println(" =============================== ");

        //对密码进行加密
        String hashpw = BCrypt.hashpw("secret", BCrypt.gensalt());
        System.out.println(hashpw);

        //校验密码
        boolean checkpw = BCrypt.checkpw("secret", "$2a$10$UeBF0Iy4RfbQGu3eC4RuaeAow/sdKWJsKAuYCBUHq4dslZ3.5lrye");
        boolean checkpw2 = BCrypt.checkpw("123", "$2a$10$HuClcUqr/FSLmzSsp9SHqe7D51Keu1sAL7tUAAcb..FyILiLdFKYy");
        System.out.println(checkpw);
        System.out.println(checkpw2);

        System.err.println(" =============================== ");

        //编码token
        String token = "c1:secret";
        String encodeToken = encodeToken(token);
        System.err.println("编码token的值: " + encodeToken);

        //解码token
        String decodeToken = decodeToken("Basic " + encodeToken);
        System.err.println("解码token值: " + decodeToken);

        System.err.println(" =============================== ");

        System.err.println("编码和解码的值是否相等: " + token.equals(decodeToken));

    }

    /**
     * 编码token
     * @param token
     * @return
     */
    private static String encodeToken(String token) throws UnsupportedEncodingException {
        byte[] encodeTokenByte;
        try {
            encodeTokenByte = Base64.getEncoder().encode(token.getBytes(StandardCharsets.UTF_8));
        } catch (IllegalArgumentException e) {
            System.err.println("解析异常");
            return null;
        }
        String encodeToken = new String(encodeTokenByte, "UTF-8");
        return encodeToken;
    }

    /**
     * 解码token
     *
     * @param encodeToken
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String decodeToken(String encodeToken) throws UnsupportedEncodingException {
        byte[] base64Token = encodeToken.substring(6).getBytes("UTF-8");
        byte[] decodedTokenByte;
        try {
            decodedTokenByte = Base64.getDecoder().decode(base64Token);
        } catch (IllegalArgumentException e) {
            System.err.println("解析异常");
            return null;
        }
        String token = new String(decodedTokenByte, "UTF-8");
        return token;
    }


}
